package com.q.tqb.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.PromtionActivity;
import com.q.tqb.adapter.PromtionAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.PromotionInfo;
import com.q.tqb.model.RankInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.CircleImageView;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class PromotionFragment extends Fragment {


    @BindView(R.id.rv_promotion)
    GRecyclerView mRvPromotion;

    Unbinder unbinder;
    @BindView(R.id.tv_day_user)
    TextView mTvDayUser;
    @BindView(R.id.tv_day_tweeter)
    TextView mTvDayTweeter;
    @BindView(R.id.tv_day_money)
    TextView mTvDayMoney;
    @BindView(R.id.tv_all_tweeter)
    TextView mTvAllTweeter;
    @BindView(R.id.iv_head2)
    CircleImageView mIvHead2;
    @BindView(R.id.tv_name2)
    TextView mTvName2;
    @BindView(R.id.tv_money2)
    TextView mTvMoney2;
    @BindView(R.id.iv_head3)
    CircleImageView mIvHead3;
    @BindView(R.id.tv_name3)
    TextView mTvName3;
    @BindView(R.id.tv_money3)
    TextView mTvMoney3;
    @BindView(R.id.iv_head1)
    CircleImageView mIvHead1;
    @BindView(R.id.tv_name1)
    TextView mTvName1;
    @BindView(R.id.tv_money1)
    TextView mTvMoney1;
    private int type;
    private int intTime;
    private String time;

    private PromtionAdapter mPromtionAdapter;

    private ArrayList<PromotionInfo> mPromotionInfos;


    public PromotionFragment() {

    }


    public static PromotionFragment newInstance(int intTime) {
        PromotionFragment fragment = new PromotionFragment();
        Bundle args = new Bundle();
        args.putInt("intTime", intTime);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            intTime = getArguments().getInt("intTime");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_promotion, container, false);
        unbinder = ButterKnife.bind(this, view);
        mTvDayUser.setSelected(true);
        mPromotionInfos = new ArrayList<>();
        mRvPromotion.setPullRefreshEnabled(false);
        mRvPromotion.setLoadingMoreEnabled(false);
        mPromtionAdapter = new PromtionAdapter(getActivity(), mPromotionInfos);
        mPromtionAdapter.setType(3);
        LinearLayoutManager linearLayoutManagert = new LinearLayoutManager(getActivity());
        mRvPromotion.setLayoutManager(linearLayoutManagert);
        mRvPromotion.setAdapter(mPromtionAdapter);

        time = getTime();
        type = 3;
        getListData();

        return view;
    }

    private String getTime() {
        switch (intTime) {
            case 0:
                return "2018-02-12 00:00:00";
            case 1:
                return "2018-02-13 00:00:00";
            case 2:
                return "2018-02-14 00:00:00";
            case 3:
                return "2018-02-15 00:00:00";
            case 4:
                return "2018-02-16 00:00:00";
            case 5:
                return "2018-02-17 00:00:00";
            case 6:
                return "2018-02-18 00:00:00";
            case 7:
                return "2018-02-19 00:00:00";
            case 8:
                return "2018-02-20 00:00:00";
            case 9:
                return "2018-02-21 00:00:00";
            case 10:
                return "2018-02-22 00:00:00";
            case 11:
                return "2018-02-23 00:00:00";
        }
        return null;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_day_user, R.id.tv_day_tweeter, R.id.tv_day_money, R.id.tv_all_tweeter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_day_user:
                type = 3;
                initSelect();
                mTvDayUser.setSelected(true);
                getListData();
                break;
            case R.id.tv_day_tweeter:
                type = 2;
                initSelect();
                mTvDayTweeter.setSelected(true);
                getListData();
                break;
            case R.id.tv_day_money:
                type = 1;
                initSelect();
                mTvDayMoney.setSelected(true);
                getListData();
                break;
            case R.id.tv_all_tweeter:
                type = 4;
                initSelect();
                mTvAllTweeter.setSelected(true);
                getListData();
                break;
            default:
                break;
        }
    }

    private void initSelect() {
        mTvAllTweeter.setSelected(false);
        mTvDayMoney.setSelected(false);
        mTvDayUser.setSelected(false);
        mTvDayTweeter.setSelected(false);
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("type", type + "");
        map.put("date", time);
        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_ACTIVITY, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<PromotionInfo> grabRedDatas = gson.fromJson(element, new TypeToken<ArrayList<PromotionInfo>>() {
                }.getType());


                mPromotionInfos.clear();
                mTvName1.setText("");
                mTvName2.setText("");
                mTvName3.setText("");

                mTvMoney1.setText("");
                mTvMoney2.setText("");
                mTvMoney3.setText("");

                mIvHead1.setImageResource(0);
                mIvHead2.setImageResource(0);
                mIvHead3.setImageResource(0);

                mPromotionInfos.addAll(grabRedDatas);

                int size = grabRedDatas.size();
                if (size != 0) {

                    mTvName1.setText(grabRedDatas.get(0).getUserInfo().getNickname());
                    if (size > 1) {
                        mTvName2.setText(grabRedDatas.get(1).getUserInfo().getNickname());
                    }
                    if (size > 2) {
                        mTvName3.setText(grabRedDatas.get(2).getUserInfo().getNickname());
                    }

                    if (type == 3 || type == 2 || type == 4) {
                        mTvMoney1.setText(grabRedDatas.get(0).getSum() + "位");
                        if (size > 1) {
                            mTvMoney2.setText(grabRedDatas.get(1).getSum() + "位");
                        }
                        if (size > 2) {
                            mTvMoney3.setText(grabRedDatas.get(2).getSum() + "位");
                        }
                    } else if (type == 1) {
                        mTvMoney1.setText(grabRedDatas.get(0).getMoney() + "元");
                        if (size > 1) {
                            mTvMoney2.setText(grabRedDatas.get(1).getMoney() + "元");
                        }
                        if (size > 2) {
                            mTvMoney3.setText(grabRedDatas.get(2).getMoney() + "元");
                        }
                    }

                    Glide
                            .with(getActivity())
                            .load(grabRedDatas.get(0).getUserInfo().getHeader())
                            .error(R.drawable.ic_head)
                            .placeholder(R.drawable.ic_head)
                            .dontAnimate()
                            .into(mIvHead1);
                    if (size > 1) {
                        Glide
                                .with(getActivity())
                                .load(grabRedDatas.get(1).getUserInfo().getHeader())
                                .error(R.drawable.ic_head)
                                .placeholder(R.drawable.ic_head)
                                .dontAnimate()
                                .into(mIvHead2);
                    }
                    if (size > 2) {
                        Glide
                                .with(getActivity())
                                .load(grabRedDatas.get(2).getUserInfo().getHeader())
                                .error(R.drawable.ic_head)
                                .placeholder(R.drawable.ic_head)
                                .dontAnimate()
                                .into(mIvHead3);
                    }
                } else {
                    Toast.makeText(getActivity(), "暂时没有数据哟！", Toast.LENGTH_SHORT).show();
                }

                mPromtionAdapter.setType(type);
                mPromtionAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
